#!/usr/bin/ksh

S=/sas/rawdata/trench/01.00.00
D=/sas/rawdata/dev/production

ACTIONS=Clear,Copy,Clean,Checkin
export PATH=/appl/smi/develop/codemgr2/bin:$PATH
export CODEMGR_LIBS=/appl/smi/develop/codemgr2/bin
export PROJECTS_ROOT=/sas/rcs/projects

META=$PROJECTS_ROOT/production/.meta

if [[ -n "$(echo $ACTIONS|awk '/Clear/ {print 1}')" ]]
then
	print cp $META/conf /tmp/conf.$$
	print rm -rf "$META"
	print mkdir -m 775 -p $META
	print cp /tmp/conf.$$ $META/conf
	print "find $PROJECTS_ROOT/production -name '*,v' -exec rm {} \\;"
fi

if [[ -n "$(echo $ACTIONS|awk '/Copy/ {print 1}')" ]]
then
	for i in $(awk -v FS=: '$1 == "SOURCE_DIR" {print $2}' /sas/rcs/projects/production/.meta/conf)
	do
		print mkdir -p $D/$i
		print print \"Copying contents of spbe895a:$S/$i/* ...\"
		print scp -q "spbe895a:$S/$i/*" $D/$i
		print rm -f "$D/$i/*.sas7bcat"
		print rm -f "$D/$i/*.log"
		print rm -f "$D/$i/*.swp"
		print rm -f "$D/$i/*.out"
		print X=\$PWD
		print cd $D/$i
		print ls -a \| "awk '/\.swp\$/ || /\.[0-9\-]{1,}\$/' | xargs -n 1 rm "
		print cd \$X
	done
fi

if [[ -n "$(echo $ACTIONS|awk '/Clean/ {print 1}')" ]]
then
	for i in $(awk -v FS=: '$1 == "SOURCE_DIR" {print $2}' /sas/rcs/projects/production/.meta/conf)
	do
		print cd $D/$i
		print print "Cleaning files in $D/$i ..."
		print /appl/smi/develop/MBNA_multenvs/jobsched/code_cleaner.pl
		print "rm *.backup"
	done
fi

if [[ -n "$(echo $ACTIONS|awk '/Checkin/ {print 1}')" ]]
then
	i=$(awk -v FS=: '$1 == "SOURCE_DIR" {print $2; exit}' /sas/rcs/projects/production/.meta/conf)
	print cd $D/$i
	print checkin --verbose
fi

exit 0
